package cn.com.dpc.service;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.util.HashSet;
import java.util.Set;

import android.util.Log;

import ch.unifr.nio.framework.AbstractChannelHandler;
import ch.unifr.nio.framework.Dispatcher;
import ch.unifr.nio.framework.FrameworkTools;

/**
 * 
 * @author liangzi
 * @version v1.0
 * @date 2008-03-20
 * @modify remove server with client confirm message
 */
public class ClientReceiveThread extends AbstractChannelHandler {

	private boolean connState = false;
	private final int PORT_NO = 8888;
	private String host = null;
	private static final java.util.logging.Logger logger = java.util.logging.Logger
			.getLogger(ClientReceiveThread.class.getName());
	private Main Main = null;
	private Dispatcher dispatcher;
	private SocketChannel channel;

	public ClientReceiveThread(String host, Main Main) {
		this.Main = Main;
		this.host = host;
	}

	public void openConnect() {
		try {
			Log.v("host:Port", host + ":" + PORT_NO);
			InetSocketAddress address = new InetSocketAddress(host, PORT_NO);
			channel = SocketChannel.open(address);
			channel.configureBlocking(false);
			dispatcher = new Dispatcher();
			dispatcher.start();
			dispatcher.registerChannel(channel, this);
			connState = true;

		} catch (IOException e) {
			System.out.println("connect remote SSManager error:"
					+ e.getMessage());
			connState = false;
			FrameworkTools.handleStackTrace(logger, e);
		}
	}

	public void channelException(Exception arg0) {
		try {
			handlerAdapter.closeChannel();
		} catch (IOException ex) {
			connState = false;
			System.out.println("channel error:" + ex.getMessage());
			FrameworkTools.handleStackTrace(logger, ex);
		}
	}

	public void sendMsg(String msg) {
		try {
			msg = msg + "#end";
			writer.transform(CharsetUtil.encodeGBK(msg));
		} catch (IOException ex) {
			connState = false;
			System.out.println("page client send Msg error:" + ex.getMessage());
			FrameworkTools.handleStackTrace(logger, ex);
		}

	}

	public void handleInput() {
		String msg = CharsetUtil.decoderGBK(reader.getBuffer());
		String[] tmpMsg = msg.split("#end");
		for (String s : tmpMsg) {
			String[] hisMsg = s.split("#");
			String msgHead = hisMsg[0];
			int msgIntHead = 0;
			boolean flag = true;
			try {
				msgIntHead = Integer.valueOf(msgHead);
			} catch (NumberFormatException e) {
				flag = false;
				System.out.println("receve error msg:" + s);
			}
			if (flag) {
				// 不是实时数据发送确认的消息
				if (msgIntHead == MessageType.RECEIVE_REAL_DATA
						|| msgIntHead == MessageType.REQUEST_RECEIVE_DATA
						|| msgIntHead == MessageType.USER_NO
						|| msgIntHead == MessageType.REQUEST_ICON_MESSAGE) {
				} else {
					sendMsg(s);
					System.out.println("send confirm msg:" + s);
				}
				Main.receive(s);
			}
		}
	}

	public void inputClosed() {
		try {
			this.handlerAdapter.closeChannel();
		} catch (IOException e) {
			connState = false;
			System.out.println("input close error" + e.getMessage());
			FrameworkTools.handleStackTrace(logger, e);
		}
	}

	public boolean isConnState() {
		return connState;
	}
}
